/**
 * @author Administrador
 * Created on 13/01/2006
 */
package br.unb.statistic.inference;

import java.io.IOException;

import br.unb.statistic.SpectrumProcess;

public class PeriodogramEstimator extends HEstimator {

    private int numSamples;
    private double[] y, x;

    public PeriodogramEstimator(Inference inference) {
        super(inference);
        this.numSamples = (int) Math.pow(2D, (int) Math.floor(Math.log(inference.getSampleSize()) / Math.log(2)));

        double[] input = new double[this.numSamples];
        double media = inference.getMean();
        try {
            inference.rewind();
            String valor = null;
            int n = 0;
            while (((valor = inference.nextValue()) != null) && (n < this.numSamples)) {
                try {
                    input[n] = Double.parseDouble(valor) / (0.1 * media);
                    n++;
                } catch (NumberFormatException nfe) {
                }
            }
            inference.getDataReader().close();
            y = SpectrumProcess.periodogram(input, this.numSamples);
        } catch (IOException ioe) {
        }

    }

    public double getHurst() {

        x = new double[(int) (y.length * 0.4)];
        for (int i = 1; i < x.length; i++) {
            y[i - 1] = Math.log(y[i]);
            x[i - 1] = Math.log((2 * Math.PI * i) / this.numSamples);
        }
        double slope = Inference.slope(x, y);
        return (1 - slope) / 2;
    }
}
